package com.neteast.androidclient.contact.adapter;

import java.util.ArrayList;
import java.util.Arrays;
import com.neteast.androidclient.contact.R;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GroupAdapter extends BaseAdapter {
	private ArrayList<String> data;
	private LayoutInflater inflater;
	public ArrayList<String> groupValues;
	
	public GroupAdapter(Context context,ArrayList<String> data) {
		this.data=data;
		inflater = LayoutInflater.from(context);
		groupValues=new ArrayList<String>();
	}
	public GroupAdapter(Context context,ArrayList<String> data,String checkedValue) {
		this(context, data);
		if (!TextUtils.isEmpty(checkedValue)) {
			groupValues.addAll(Arrays.asList(checkedValue.split(",")));
		}
	}
	public int getCount() {
		return data.size();
	}

	public String getItem(int position) {
		return data.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	public void setCheckedValue(String checkedValue) {
		groupValues.clear();
		if (!TextUtils.isEmpty(checkedValue)) {
			groupValues.addAll(Arrays.asList(checkedValue.split(",")));
			notifyDataSetChanged();
		}
	}
	
	public String getCheckedValue() {
		int size = groupValues.size();
		StringBuilder sb=new StringBuilder();
		for (int i = 0; i < size; i++) {
			sb.append(groupValues.get(i)).append(",");
		}
		if (sb.length()>0) {
			sb.deleteCharAt(sb.length()-1);
		}
		return sb.toString();
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = inflater.inflate(R.layout.group_item, null);
		TextView groupLabel = (TextView) convertView.findViewById(R.id.group_label);
		CheckBox groupHander = (CheckBox) convertView.findViewById(R.id.group_hander);
		String value = data.get(position);
		groupLabel.setText(value);
		groupHander.setChecked(groupValues.contains(value));
		groupHander.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				LinearLayout ll=(LinearLayout) buttonView.getParent();
				TextView tvLabel=(TextView) ll.getChildAt(0);
				String value = tvLabel.getText().toString();
				if (isChecked) {
					addGroup(value);
				}else{
					groupValues.remove(value);
				}
			}
		});
		return convertView;
	}
	
	public void addGroup(String value){
		if (groupValues.contains(value)) {
			return;
		}
		groupValues.add(value);
		Log.i("test", "groupValues="+groupValues.toString());
	}
	
	public void removeGroup(String value){
		if (groupValues.contains(value)) {
			groupValues.remove(value);
		}
		Log.i("test", "groupValues="+groupValues.toString());
	}
}
